home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / pbaseiv.zip / P4DOS010.TIP < prev    next >
Text File  |  1991-12-16  |  4KB  |  118 lines

  1. [This tip contains an example of a technique that looked
  2. good on the surface, but actually turned out to have a
  3. serious flaw. Read on, and see if you can figure out what it
  4. is before you get to the end.]
  5.  
  6. It's frustrating to deal with uppercase and lowercase
  7. arguments passed to batch files--`PROG file', `PROG FILE',
  8. or even `ProG FiLe', for example. Testing for all
  9. combinations of case (making the batch program case
  10. insensitive) usually requires a series of messy IF
  11. statements or complex FOR loops.
  12.  
  13. But I've hit upon an approach that lets batch files convert
  14. arguments to uppercase before using them. The technique
  15. relies on a side effect of the DOS PATH command, which,
  16. automatically converts its parameter to uppercase before
  17. storing it in the environment variable. By saving the
  18. current PATH, using it to process parameters, and then
  19. restoring the PATH, this trick makes a handy case converter
  20. in any batch file.
  21.  
  22. A sample batch file, RUN.BAT (see listing), converts
  23. arguments to uppercase, then runs a program. First, it
  24. checks that you've entered at least two items: a program to
  25. run, and any arguments for that program. After this, it
  26. saves PATH in OLDPATH and sets RUN to the target program's
  27. name. A loop then uses SHIFT to build a string in PATH out
  28. of each argument. This converts the arguments to uppercase.
  29. After the loop, PATH is set to OLDPATH, which is erased to
  30. conserve environment space. The %RUN% command then runs the
  31. program with the converted arguments.
  32.  
  33. William T. McGowan III
  34. Columbia, South Carolina
  35.  
  36. Editor's note: For a sample of how you can use this clever
  37. idea, run TESTPROG.BAT (see second listing below). The batch
  38. file simulates a program that recognizes two fictitious
  39. commands, OPEN and CLOSE.
  40.  
  41. When you enter testprog OPEN or testprog CLOSE, TESTPROG
  42. confirms that it received those commands. If you enter those
  43. same commands in lowercase, the program displays an error
  44. message. But when you use Mr. McGowan's batch file to run
  45. TESTPROG, you can type arguments using any combination of
  46. lowercase and uppercase letters.
  47.  
  48. If you get an `Out of environment space' message, add the
  49. line shell command.com /e:384 /p to the end of your
  50. CONFIG.SYS file. When you reboot, DOS will set aside 384
  51. bytes (instead of the default 256) for environment
  52. variables. If this is still not enough, try 512 or more. The
  53. `/p' makes the change to COMMAND.COM permanent.
  54.  
  55. Postscript: This tip, which appeared in the January 1990
  56. issue of PC World, looks good on the surface, but it carries
  57. with it a serious potential pitfall. Can you see what it is?
  58. Answer: the user's path will be destroyed if he breaks out
  59. of the batch while the path holds a string that's being
  60. case-converted. What's worse, the sample batch file spends a
  61. lot of time in a loop, during which the PATH isn't set to
  62. anything valid.
  63.  
  64. Luckily, there's a better and simpler way to make a batch
  65. file ignore case. The GOTO command, when used with an
  66. environment variable or a command line parameter (e.g. GOTO
  67. %1) will convert its parameter to uppercase before jumping.
  68. This is much safer, because it can never destroy your path
  69. and eliminates the need to test every possible spelling of
  70. every parameter.
  71.  
  72.  
  73. RUN.BAT demonstrates a not-too-safe way to use PATH: as an
  74. uppercase converter in batch files. To avoid leaving a user
  75. with an invalid path, we recommend using the GOTO command
  76. on a parameter or an environment variable in situations
  77. where case needs to be ignored.
  78.  
  79. @echo off
  80. if "%2"=="" goto HELP
  81. set OLDPATH=%PATH%
  82. set RUN=%1
  83. :NEXT
  84. shift
  85. PATH=%1
  86. set RUN=%RUN% %PATH%
  87. if not "%2"=="" goto NEXT
  88. set PATH=%OLDPATH%
  89. set OLDPATH=
  90. %RUN%
  91. :HELP
  92. echo Enter run x arg1 arg2...
  93. echo where x is a program and
  94. echo args are option arguments
  95.  
  96. TESTPROG.BAT: Original test program for RUN.BAT
  97.  
  98. @echo off
  99. if "%1"=="" goto ERROR
  100. if %1==OPEN goto OPEN
  101. if %1==CLOSE goto CLOSE
  102. goto ERROR
  103. :OPEN
  104. echo You selected OPEN
  105. goto END
  106. :CLOSE
  107. echo You selected CLOSE
  108. goto END
  109. :ERROR
  110. echo Bad or missing command
  111. :END
  112.  
  113. Title: Insensitive Batch Arguments
  114. Category: DOS
  115. Issue date: Jan 1991
  116. Editor: Tom Swan
  117. Supplementary files: NONE
  118.